﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RadarMonitor.CIMISS;
using RadarMonitor.DB;
using RadarMonitor;

namespace RadarMonitor.ViewModel
{
   
    public class LogCardViewModel : INotifyPropertyChanged
    {
        public LogCardViewModel()
        {
            update();
        }

        private int  RowCntPerPage =10; //每页显示多少行

        private int _pageNum=1;

        public int PageNum
        {
            get { return _pageNum; }
            set { _pageNum = value;
                OnPropertyChanged("PageNum");
            }
        }

        private int _PageCnt=1;

        public int PageCnt
        {
            get { return _PageCnt; }
            set { _PageCnt = value;
                OnPropertyChanged("PageCnt");
            }
        }

        private List<logItem> _CurrentRecords;

        public List<logItem> CurrentRecords
        {
            get { return _CurrentRecords; }
            set { _CurrentRecords = value;
                OnPropertyChanged("CurrentRecords");
            }
        }

        public void update()
        {
            //int rowCnt = DBHelper.Instance.RowCount(DBHelper.MonidorTabel);
            //double pageCnt = (double)rowCnt / (double)RowCntPerPage;
            //PageCnt = (int)Math.Ceiling(pageCnt);
            //if (pageCnt == 0) pageCnt = 1;

            ////if(PageNum == 1) //刷新
            ////{
            ////    var result = DBHelper.Instance.logItemsAt((PageNum - 1) * RowCntPerPage, RowCntPerPage);
            ////    CurrentRecords = new List<logItem>(result);
            ////}


            bool soureEn = false;
            RadarMonitorSource source = RadarMonitorSource.BaseFTP;
            converSourceFilter(ref soureEn, ref source);

            bool STEn = false;
            DateTime ST = DateTime.Now;
            bool ETEn = false;
            DateTime ET = DateTime.Now;
            converDateFilter(ref STEn,ref ST, ref ETEn, ref ET);


            int rowCnt = DBHelper.Instance.RowCount( soureEn, (int)source, STEn, ST, ETEn, ET);
            double pageCnt = (double)rowCnt / (double)RowCntPerPage;
            PageCnt = (int)Math.Ceiling(pageCnt);
            if (pageCnt == 0) pageCnt = 1;

            PageNum = 1;
    
            var result = DBHelper.Instance.logItemsAt((PageNum - 1) * RowCntPerPage, RowCntPerPage, soureEn, (int)source, STEn, ST, ETEn, ET);
            CurrentRecords = new List<logItem>(result);
           


        }

        public void refresh()
        {
            if(PageNum >= 1)
            {
                bool soureEn = false;
                RadarMonitorSource source = RadarMonitorSource.BaseFTP;
                converSourceFilter(ref soureEn, ref source);

                bool STEn = false;
                DateTime ST = DateTime.Now;
                bool ETEn = false;
                DateTime ET = DateTime.Now;
                converDateFilter(ref STEn, ref ST, ref ETEn, ref ET);

                var result = DBHelper.Instance.logItemsAt((PageNum - 1) * RowCntPerPage, RowCntPerPage, soureEn, (int)source, STEn, ST, ETEn, ET);
                CurrentRecords = new List<logItem>(result);
            }
           
        }

        public void nextPage()
        {
            gotoPage(PageNum + 1);
        }

        public void piorPage()
        {
            gotoPage(PageNum - 1);
        }

        public void gotoPage(int page)
        {
            if(page > PageCnt || page <1 || page == PageNum)
            {
                return;
            }
          
            else
            {

                bool soureEn = false;
                RadarMonitorSource source = RadarMonitorSource.BaseFTP;
                converSourceFilter(ref soureEn, ref source);

                bool STEn = false;
                DateTime ST = DateTime.Now;
                bool ETEn = false;
                DateTime ET = DateTime.Now;
                converDateFilter(ref STEn, ref ST, ref ETEn, ref ET);

                PageNum = page;
                var result = DBHelper.Instance.logItemsAt((PageNum - 1) * RowCntPerPage, RowCntPerPage, soureEn, (int)source, STEn, ST, ETEn, ET);
                CurrentRecords = new List<logItem>(result);
            }
        }

        public int sourceFilter =0;
        public int dateFilter  =0 ;

        private void converSourceFilter(ref bool filter, ref RadarMonitorSource source)
        {
            filter = (sourceFilter != 0 || sourceFilter> 5);

            if (filter)
            {
                source = (RadarMonitorSource)(sourceFilter - 1);
            }
        }

        private void converDateFilter(ref bool filterS, ref DateTime St, ref bool filterE, ref DateTime Se)
        {
            if (dateFilter ==0 || dateFilter >4)
            {
                filterS = false;
                filterE = false;
                return;
            }

            if (dateFilter == 1) // 最近一天
            {
                filterS = true;
                filterE = false;
                St = DateTime.Now.AddDays(-1);
            }
            else if (dateFilter == 2) //最近一周
            {
                filterS = true;
                filterE = false;
                St = DateTime.Now.AddDays(-7);
            }

            else if (dateFilter == 3) //最近一月
            {
                filterS = true;
                filterE = false;
                St = DateTime.Now.AddDays(-30);
            }

            else if (dateFilter ==4) // 更早
            {
                filterS = false;
                filterE = true;
                Se = DateTime.Now.AddDays(-30);
            }
        }


        #region 属性变化通知
        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string propertyName = "")
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }



        #endregion
    }
}
